﻿<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="../helpproject.xsl" ?>
<topic template="Default" lasteditedby="Geert" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../helpproject.xsd">
  <title translate="true">Specifications</title>
  <keywords>
    <keyword translate="true">Specification</keyword>
  </keywords>
  <body>
    <header>
      <para styleclass="Heading1"><text styleclass="Heading1" translate="true">Specifications</text></para>
    </header>
    <para styleclass="Normal"><text styleclass="Normal" translate="true">Catel provides an implementation of the </text><link displaytype="text" defaultstyle="true" type="weblink" href="http://en.wikipedia.org/wiki/Specification_pattern" target="_blank" styleclass="Normal" translate="true">specification pattern</link><text styleclass="Normal" translate="true">.</text></para>
    <para styleclass="Normal"><text styleclass="Normal" translate="true">A specification pattern outlines a business rule that is combinable with other business rules. In this pattern, a unit of business logic inherits its functionality from the abstract aggregate </text><text styleclass="Normal" style="font-style:italic;" translate="true">Composite Specification</text><text styleclass="Normal" translate="true"> class. The </text><text styleclass="Normal" style="font-style:italic;" translate="true">Composite Specification</text><text styleclass="Normal" translate="true"> class has one method called </text><text styleclass="Normal" style="font-style:italic;" translate="true">IsSatisfiedBy</text><text styleclass="Normal" translate="true"> that returns a boolean value. </text></para>
    <para styleclass="Normal"><text styleclass="Normal" translate="true">After instantiation, the specification is &quot;chained&quot; with other specifications, making new specifications easily maintainable, yet highly customizable business logic. Furthermore upon instantiation the business logic may, through method invocation or inversion of control, have its state altered in order to become a delegate of other classes such as a persistence repository.</text></para>
    <para styleclass="Normal"><text styleclass="Normal" translate="true">The big advantage is that commonly used queries can be converted to a specification or a combination of specifications. Then if the query should change (for example, </text><text styleclass="Normal" style="font-style:italic;" translate="true">IsDeleted</text><text styleclass="Normal" translate="true"> is introduced), only the specification needs to be changed.</text></para>
    <para styleclass="Normal"><text styleclass="Normal" translate="true">The cool thing about the specification implementation in Catel is that it can be used for Entity Framework queries, but also provides implicit casting to a </text><text styleclass="Normal" style="font-style:italic;" translate="true">Func&lt;TEntity, bool&gt;</text><text styleclass="Normal" translate="true">. This way a specification can be passed to any method accepting a method and thus also works with all linq queries on normal collections.</text></para>
    <para styleclass="Heading1"><text styleclass="Heading1" translate="true">Using the classes</text></para>
    <para styleclass="Normal"><text styleclass="Normal" translate="true">Creating a specification is very simple. Below is an example of an active products specification:</text></para>
    <para styleclass="Code Example"><text styleclass="Code Example" style="font-weight:bold; font-style:normal; color:#000000;" translate="true">public</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#ffffff;" translate="true">&#32;</text><text styleclass="Code Example" style="font-weight:bold; font-style:normal; color:#000000;" translate="true">class</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#ffffff;" translate="true">&#32;</text><text styleclass="Code Example" translate="true">ActiveProductSpecification</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#ffffff;" translate="true">&#32;</text><text styleclass="Code Example" translate="true">:</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#ffffff;" translate="true">&#32;</text><text styleclass="Code Example" translate="true">Specification&lt;Product&gt;</text><br/><text styleclass="Code Example" translate="true">{</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#ffffff;" translate="true"> &#160; &#160;</text><text styleclass="Code Example" style="font-weight:bold; font-style:normal; color:#000000;" translate="true">public</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#ffffff;" translate="true">&#32;</text><text styleclass="Code Example" translate="true">ActiveProductSpecification()</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#ffffff;" translate="true">&#32;</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#ffffff;" translate="true"> &#160; &#160; &#160; &#160;</text><text styleclass="Code Example" translate="true">:</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#ffffff;" translate="true">&#32;</text><text styleclass="Code Example" style="font-weight:bold; font-style:normal; color:#000000;" translate="true">base</text><text styleclass="Code Example" translate="true">(x</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#ffffff;" translate="true">&#32;</text><text styleclass="Code Example" translate="true">=&gt;</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#ffffff;" translate="true">&#32;</text><text styleclass="Code Example" translate="true">!x.IsDeleted</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#ffffff;" translate="true">&#32;</text><text styleclass="Code Example" translate="true">&amp;&amp;</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#ffffff;" translate="true">&#32;</text><text styleclass="Code Example" translate="true">x.IsActive)</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#ffffff;" translate="true"> &#160; &#160;</text><text styleclass="Code Example" translate="true">{</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#ffffff;" translate="true"> &#160; &#160;</text><text styleclass="Code Example" translate="true">}</text><br/><text styleclass="Code Example" translate="true">}</text></para>
    <para styleclass="Normal"><text styleclass="Normal" translate="true">Then the specification can be used like this:</text></para>
    <para styleclass="Code Example"><text styleclass="Code Example" translate="true">var</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#ffffff;" translate="true">&#32;</text><text styleclass="Code Example" translate="true">productRepository</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#ffffff;" translate="true">&#32;</text><text styleclass="Code Example" translate="true">=</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#ffffff;" translate="true">&#32;</text><text styleclass="Code Example" style="font-weight:bold; font-style:normal; color:#000000;" translate="true">new</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#ffffff;" translate="true">&#32;</text><text styleclass="Code Example" translate="true">ProductRepository();</text><br/><text styleclass="Code Example" translate="true">var</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#ffffff;" translate="true">&#32;</text><text styleclass="Code Example" translate="true">activeProductSpecification</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#ffffff;" translate="true">&#32;</text><text styleclass="Code Example" translate="true">=</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#ffffff;" translate="true">&#32;</text><text styleclass="Code Example" style="font-weight:bold; font-style:normal; color:#000000;" translate="true">new</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#ffffff;" translate="true">&#32;</text><text styleclass="Code Example" translate="true">ActiveProductSpecification();</text><br/><br/><text styleclass="Code Example" translate="true">var</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#ffffff;" translate="true">&#32;</text><text styleclass="Code Example" translate="true">activeProducts</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#ffffff;" translate="true">&#32;</text><text styleclass="Code Example" translate="true">=</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#ffffff;" translate="true">&#32;</text><text styleclass="Code Example" translate="true">productRepository.GetQuery(activeProductSpecification);</text></para>
  </body>
</topic>
